#include <QCoreApplication>
#include <QDir>
#include <QTemporaryFile>
#include <QTextCodec>

#include "cclreader.h"
#include "effectivenesstester.h"
#include "restclient.h"

#include <QDebug>

#define PATH_TO_CCL_DATA    "../../data/ccl"
#define PATH_TO_JSON_DATA   "../../data/json"
#define PATH_TO_QUERIES     "../../data/queries/examples"
#define PATH_TO_STOPLIST    "../../data/stoplist/stoplist"

QStringList readStoplist();

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF−8"));
    bool done=false;
    if (argc<2)
    {
        qDebug() << "Use -p for parsing, -i for indexing, -q for querrying\nWrite query (or \"exit\" to quit)\n";

        RESTClient *client = new RESTClient();
        QStringList stoplist(readStoplist());
        QTextStream in(stdin);
        QTextStream out(stdout);
        QString line;
        while((line = in.readLine()) != "exit"){
            out << client->search(line, stoplist) << "\n";
            out.flush();
        }
        done = false;
    }
    QStringList args = a.arguments();
    if (args.contains("-p"))
    {
            // ccl -> json
            CCLReader cclReader;
            QDir cclDir(PATH_TO_CCL_DATA);
            cclDir.setFilter(QDir::Files);
            foreach(const QString &entry, cclDir.entryList()){
                QFile doc(QString(PATH_TO_JSON_DATA) + "/" + entry.mid(4, entry.size() - 8) + ".json");
                cclReader.readAsJSON(QString(PATH_TO_CCL_DATA) + "/" + entry, doc);
                qDebug() << entry;
            }
            done=true;
    }
    if (args.contains("-i"))
    {
            // json -> elasticsearch
            RESTClient *client = new RESTClient();
            QObject::connect(client, SIGNAL(finished()), qApp, SLOT(quit()));

            // delete docs
            client->clearIndex();

            // update mapping
            client->setSimilarity(args[args.indexOf("-i") + 1]);

            // index docs
            QDir jsonDir(PATH_TO_JSON_DATA);
            jsonDir.setFilter(QDir::Files);
            client->putAllDocuments(jsonDir);
            done=true;
    }
    if (args.contains("-q"))
    {
            // search tests
            QDir queriesDir(PATH_TO_QUERIES);
            queriesDir.setFilter(QDir::Files);
            QString resultNumberS = args.indexOf("-q") < args.size() - 1 ? args[args.indexOf("-q") + 1] : "";

            bool ok;
            int resultNumber = resultNumberS.toInt(&ok);
            if(!ok)
                resultNumber = 20;
            QStringList stoplist(readStoplist());
            (new EffectivenessTester(queriesDir, stoplist, resultNumber))->start();
            done=true;
    }
    if(args.contains("-d"))     //debug
    {

    }
    return done?a.exec():0;
}

QStringList readStoplist(){
    QStringList stoplist;
    QFile stopfile(PATH_TO_STOPLIST);
    stopfile.open(QIODevice::ReadOnly);
    QTextStream in(&stopfile);
    QString line="";
    while(! (line = in.readLine()).isEmpty())
        stoplist << line;
    stopfile.close();
    return stoplist;
}
